package com.lagou.edu.controller;

import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.pojo.LagouUser;
import com.lagou.edu.pojo.Result;
import com.lagou.edu.servce.TokenService;
import com.lagou.edu.servce.UserService;
import com.lagou.edu.service.CodeService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.UUID;

/**
 * @Author 梅松
 * @Date 2021/10/17 10:04
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @Reference
    private CodeService codeService;
    @Autowired
    private TokenService tokenService;

    /**
     * 获取验证码
     * @param email
     * @return
     */
    @GetMapping("/createCheckCode")
    public Result<String> createCheckCode( @NotBlank String email){
        return Result.ok(codeService.findCodeByEmail(email));
    }

    /**
     * 注册
     * @param email
     * @param password
     * @param code
     * @return
     */
    @GetMapping("/register")
    public Result<Boolean> register(@NotBlank String email,
                           @NotBlank String password,
                          @NotBlank String code){


        //1、调用codeservice的校验生成码接口，如果校验失败，返回生成码错误信息
        Result result = codeService.checkCode(email, code);

        if(!result.getCode().equals("200")){
            return result;
        }

        //2、校验email是否已经注册
        boolean flag = userService.isRegistered(email);
        if(flag){
            return Result.fail("这个邮箱已经注册过了");
        }

        //2、保存用户信息到数据库
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        lagouUser = userService.saveUser(lagouUser);
        if(lagouUser == null){
            return Result.fail("注册用户失败");
        }
        //3、如果保存成功，删除注册码信息
        codeService.deleteCode(email);
        return Result.ok("注册成功");
    }

    /**
     * 根据email判断是否已经注册
     * @param email
     * @return
     */
    @RequestMapping("/isRegistered/{email}")
    public Result<Boolean> isRegistered(@PathVariable @NotBlank  String email){
        return Result.ok(userService.isRegistered(email));
    }

    /**
     * 登录接口
     * @param email
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public Result<Boolean> login(@NotBlank  String email,
                                @NotBlank String password, HttpServletResponse response){
       //1、匹配email和密码判断登录
       boolean isLogin = userService.login(email,password);
        //2、登录失败返回错误信息
        if(!isLogin){
            return Result.fail("用户名或密码错误，请重新输入！");
        }
        //3、登录成功
        //4、保存token
        //创建token
        LagouToken lagouToken = new LagouToken();
        String token = UUID.randomUUID().toString();
        lagouToken.setToken(token);
        lagouToken.setEmail(email);
        boolean isSave = tokenService.save(lagouToken);
        if(!isSave){
            return Result.fail("保存token失败");
        }
        //5、将token放入cookie返回
        Cookie cookie = new Cookie("token",token);
        cookie.setHttpOnly(false);
        cookie.setPath("/");
        response.addCookie(cookie);
        return Result.ok("登录成功");
    }

    @RequestMapping("/info/{token}")
    public Result<LagouToken> info(@PathVariable  String token){
        return Result.ok(tokenService.findByToken(token));
    }
}
